﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;

public partial class controls_login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] ==null)
        {
            PanelLogin.Visible = true;
            PanelLogout.Visible = false;
        }
        else
        {
            PanelLogin.Visible = false;
            PanelLogout.Visible = true;
            lbName.Text = Session["Username"].ToString();
        }
    }

    protected void btnDangNhap_Click(object sender, EventArgs e)
    {
        UserBLL usr = new UserBLL();
        string hashedpass = EncryptMD5(txtPassword.Text.Trim());
        if (usr.checkLogin(txtUsername.Text.Trim(), hashedpass))
        {
            PanelLogin.Visible = false;
            PanelLogout.Visible = true;
            lbName.Text = txtUsername.Text.ToString();
            Session["Username"] = txtUsername.Text;
        }
        else
        {
            lbKQ.Text = "Thông tin đăng nhập sai!";
        }
    }
    protected void btnThoat_Click(object sender, EventArgs e)
    {
        Session["Username"] = null;
        PanelLogin.Visible = true;
        PanelLogout.Visible = false;
    }
    // Method Encryp password
    public string EncryptMD5(string data)
    {
        MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
        byte[] b = System.Text.Encoding.UTF8.GetBytes(data);
        b = MD5.ComputeHash(b);
        StringBuilder s = new StringBuilder();
        foreach (byte p in b)
        {
            s.Append(p.ToString("x").ToLower());
        }
        return s.ToString();
    }
}